package com.edu.hre.struts.action.recruit;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.edu.hre.common.utils.BeanFinder;
import com.edu.hre.common.utils.ComboBoxInit;
import com.edu.hre.common.utils.CommonUtils;
import com.edu.hre.common.utils.StringConstants;
import com.edu.hre.db.pojo.HreCandidateSkill;
import com.edu.hre.db.service.BaseService;
import com.edu.hre.db.utils.Compare;
import com.edu.hre.struts.form.recruit.CanSkillUpdateForm;

public class CanSkillUpdateAction extends DispatchAction{
	
	private static final Logger logger = Logger.getLogger(CanSkillUpdateAction.class);
	private static final BaseService baseService = BeanFinder.getBean(BaseService.BEAN_NAME, BaseService.class);
	private final String ACTION_NAME = "canSkillUpdate";
	
	public ActionForward add(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		CommonUtils.println(CommonUtils.getSourceCodeLocation());
		CanSkillUpdateForm beanForm = (CanSkillUpdateForm) form;
		beanForm.setIsAddNew(true);

		if(request.getParameter(StringConstants.BTN_ADD) == null){
			
			int canNum = CommonUtils.getIntParameter("can_id", request);
			beanForm.setCanId(canNum);

			beanForm.setMessage(StringConstants.BLANK);
			initFormBean(beanForm);
			return mapping.findForward(ACTION_NAME);
		}
		
		CandidateUpdateAction.setAttributesForRequest(beanForm.getCanId(), request);
		
		String message = validateForm(beanForm);
		if(!StringUtils.isBlank(message)){
			beanForm.setMessage(message);
			initFormBean(beanForm);
			return mapping.findForward(ACTION_NAME);
		}

		byte year = verifyYear(beanForm.getYear());

		HreCandidateSkill tmp = new HreCandidateSkill();
		tmp.setCandidateId(beanForm.getCanId());
		tmp.setSkillId(beanForm.getSkill());
		tmp.setYearsOfExp((byte) year);
		tmp.setComments(CommonUtils.normalizeString(beanForm.getComment()));
		tmp.setDateCreated(new Date());
		tmp.setDateModified(new Date());

		boolean status = baseService.saveOrUpdate(tmp);
		if (!status) {
			beanForm.setMessage(StringConstants.INSERT_FAILURE_MESSAGE);
			initFormBean(beanForm);
			return mapping.findForward(ACTION_NAME);
		}

		response.getWriter().write(StringConstants.POP_UP_CLOSE_SCRIPT);
		return null;
	}

	public ActionForward edit(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		CommonUtils.println(CommonUtils.getSourceCodeLocation());
		CanSkillUpdateForm beanForm = (CanSkillUpdateForm) form;
		beanForm.setIsAddNew(false);
		
		if(request.getParameter(StringConstants.BTN_EDIT) == null){

			int id = CommonUtils.getIntParameter("id", request);
			HreCandidateSkill tmp = baseService.getFirstPojo("id", Compare.EQUAL, (long) id, null, null, 0, HreCandidateSkill.class);
			
			if (tmp == null) {
				beanForm.setMessage(StringConstants.BLANK);
				initFormBean(beanForm);
				return mapping.findForward(ACTION_NAME);
			}
			
			beanForm.setId(id);
			beanForm.setCanId(tmp.getCandidateId());
			beanForm.setSkill(tmp.getSkillId());
			beanForm.setYear(tmp.getYearsOfExp() + StringConstants.BLANK);
			beanForm.setComment(CommonUtils.normalizeString(tmp.getComments()));

			beanForm.setMessage(StringConstants.BLANK);
			initFormBean(beanForm);
			return mapping.findForward(ACTION_NAME);
		}
		
		CandidateUpdateAction.setAttributesForRequest(beanForm.getCanId(), request);
		
		byte year = verifyYear(beanForm.getYear());
		HreCandidateSkill tmp = baseService.getFirstPojo("id", Compare.EQUAL, (long) beanForm.getId(), null, null, 0, HreCandidateSkill.class);

		if (tmp == null) {
			beanForm.setMessage(StringConstants.BLANK);
			initFormBean(beanForm);
			return mapping.findForward(ACTION_NAME);
		}

		tmp.setSkillId(beanForm.getSkill());
		tmp.setYearsOfExp((byte) year);
		tmp.setComments(CommonUtils.normalizeString(beanForm.getComment()));
		tmp.setDateModified(new Date());

		boolean status = baseService.saveOrUpdate(tmp);
		if (!status) {
			beanForm.setMessage(StringConstants.UPDATE_FAILURE_MESSAGE);
			initFormBean(beanForm);
			return mapping.findForward(ACTION_NAME);
		}

		response.getWriter().write(
				 "<script language=\"javascript\">\n" +
						" parent.dw.close();" + 
						 " var param = parent.document.createElement('input'); " +
						 " param.type = 'hidden'; " +
						 " param.name = 'id'; " +
						 " param.value = " + tmp.getCandidateId() +  ";" +
						 " parent.document.forms[0].appendChild(param);" +
						" parent.document.forms[0].submit(); \n" +
				 "</script>");
		return null;
	}

	private byte verifyYear (String yearStr) {
		try {
			return Byte.parseByte(yearStr);
		} catch (Exception e) {
			return -1;
		}
	}
	
	private void initFormBean (CanSkillUpdateForm beanForm) {
		
		beanForm.setAllSkills(ComboBoxInit.getAllSkills(false));
	}
	
	private String validateForm (CanSkillUpdateForm beanForm) {
		
		byte year = verifyYear(beanForm.getYear());
		if (year <= 0) {
			return "Year must be a positive integer and not exceed 80.";
		}
		
		if (year > 80) {
			return "Year must not exceed 80.";
		}

		return StringConstants.BLANK;
	}
}
